<?php

namespace App\Http\Controllers;

use App\Models\Operation;
use Illuminate\Http\Request;

class OperationController extends Controller
{
    public function index(Request $request)
    {
        $begin = $request->input('date_begin'); 
        $end = $request->input('date_end'); 
        if($begin){
            $operation = Operation::requestCriteria($request)->whereBetween('time', [$begin, $end])->paginate($request->query('limit'));
        }else{
            $operation = Operation::requestCriteria($request)->paginate($request->query('limit'));
        }
        return $this->respondSuccess($operation);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'user_name' => 'required',
            'type' => 'required',
            'description' => 'required',
        ]);
        $operation = Operation::create($request->input());
        return $this->respondSuccess($operation);
    }

    public function show(Request $request, Operation $operation)
    {
        return $this->respondSuccess($operation->load(['alarm', 'ESCluster']));
    }

    public function update(Request $request, Operation $operation)
    {
        $data = $operation->update($request->input());
        return $this->respondSuccess($data);
    }
    
    public function destroy(Operation $operation)
    {
        $data = $operation->delete();
        return $this->respondSuccess($data);
    }
}
